from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
import time
import csv

"""
    @author: 王胜
    @version: 1.0

"""
driver = Chrome()

driver.get('https://data.eastmoney.com/bbsj/yjbb/600811.html')
time.sleep(10)

# 基本每股收益: basic_eps
basic_eps = driver.find_elements(By.XPATH, "//*[@id='dataview']/div[2]/div[2]/table/tbody/tr/td[2]")
basic_eps = [obj.text for obj in basic_eps]
# 扣非每股收益: deducted_eps
deducted_eps = driver.find_elements(By.XPATH, "//*[@id='dataview']/div[2]/div[2]/table/tbody/tr/td[3]")
deducted_eps = [obj.text for obj in deducted_eps]
# 营业总收入: total_revenue
total_revenue = driver.find_elements(By.XPATH, "//*[@id='dataview']/div[2]/div[2]/table/tbody/tr/td[4]")
total_revenue = [obj.text for obj in total_revenue]
# 营业总收入同比增长
revenue_growth_rate = driver.find_elements(By.XPATH, "//*[@id='dataview']/div[2]/div[2]/table/tbody/tr/td[5]/span")
revenue_growth_rate = [obj.text for obj in revenue_growth_rate]
# 营业总收入滚动环比增长
revenue_rolling_growth_rate = driver.find_elements(By.XPATH,
                                                   "//*[@id='dataview']/div[2]/div[2]/table/tbody/tr/td[6]/span")
revenue_rolling_growth_rate = [obj.text for obj in revenue_rolling_growth_rate]
# 每股净资产
net_assets_per_share = driver.find_elements(By.XPATH, "//*[@id='dataview']/div[2]/div[2]/table/tbody/tr/td[10]")
net_assets_per_share = [obj.text for obj in net_assets_per_share]
# 净资产收益率
net_profit_margin = driver.find_elements(By.XPATH, "//*[@id='dataview']/div[2]/div[2]/table/tbody/tr/td[11]")
net_profit_margin = [obj.text for obj in net_profit_margin]
# 毛利率
gross_profit = driver.find_elements(By.XPATH, "//*[@id='dataview']/div[2]/div[2]/table/tbody/tr/td[13]")
gross_profit = [obj.text for obj in gross_profit]

headers = ["基本每股收益", "扣非每股收益", "营业总收入", "营业总收入同比增长", "营业总收入滚动环比增长", "每股净资产",
           "净资产收益率", "毛利率"]
column_data = list(
    zip(basic_eps, deducted_eps, total_revenue, revenue_growth_rate, revenue_rolling_growth_rate, net_assets_per_share,
        net_profit_margin, gross_profit))
# 指定要写入的文件名
filename = "quarterly.csv"
with open(filename, 'w', newline='', encoding='utf-8') as csvfile:
    csv_writer = csv.writer(csvfile)
    #     写入表头
    csv_writer.writerow(headers)
    #     写入数据
    csv_writer.writerows(column_data)

